import { message } from "antd";
import { NoticeType } from "antd/es/message/interface";
import throttle from "lodash/throttle";

const THROTTLE_TIME = 3000; // 节流时间，单位为毫秒

// 创建一个节流消息函数
const createThrottledMessage = (type: NoticeType) => {
  return throttle(
    (content: string, duration: number = 3, onClose?: () => void) => {
      message.open({
        type,
        content,
        duration, // 消息显示时长，单位是秒
        onClose,
      });
    },
    THROTTLE_TIME
  );
};

// 定义消息类型
const messagetype: NoticeType[] = [
  "success",
  "error",
  "info",
  "warning",
  "loading",
];

// 重写每种类型的 message 方法并应用节流
messagetype.forEach((type) => {
  const throttledMessage = createThrottledMessage(type); // 得到节流函数
  (message as any)[type] = (
    content: string,
    duration: number = 3,
    onClose?: () => void
  ) => {
    throttledMessage(content, duration, onClose);
  };
});
